<?php

namespace App\Admin\Controllers;

use App\Admin\Renderable\UserTable;
use App\Admin\Repositories\Ad;
use App\Models\AdCategory;
use App\Models\AdHierarchy;
use App\Models\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class AdController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Ad(['user:id,avatar,nickname','category:id,name','hierarchy:id,name']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('user.avatar','用户头像')->image('',80,80);
            $grid->column('user.nickname','用户昵称');
            $grid->column('trade_no');
            $grid->column('name','广告名称');
            $grid->column('category.name','广告类别');
            $grid->column('type')
                 ->using([1 => '图片广告',2 => '视频广告'])
                 ->label([1 => 'success',2 => 'info']);
            $grid->column('hierarchy.name','广告层级');
            $grid->column('area');
            $grid->column('days');
            $grid->column('amount');
            $grid->column('audit_status')
                ->using([1 => '待审核',2 =>'已通过',3 => '已拒绝'])
                ->label([1 => 'danger',2 => 'success',3 => 'warning']);
            $grid->column('status')
                ->using([1 => '待付款',2 => '待投放',3 => '投放中',4 => '已完成',5 => '已取消'])
                ->label([1 => 'danger',2 => 'warning',3 => 'info',4 =>'success',5 => 'dark']);

            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->fixColumns(1);

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Ad(), function (Show $show) {
            $show->field('id');
            $show->field('ad_category_id');
            $show->field('type');
            $show->field('ad_hierarchy_id');
            $show->field('area');
            $show->field('days');
            $show->field('amount');
            $show->field('Instructions');
            $show->field('material');
            $show->field('audit_status');
            $show->field('status');
            $show->field('order_no');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Ad(), function (Form $form) {
            $form->display('id');
            $form->selectTable('user_id','用户')
                ->from(UserTable::make())
                ->model(User::class,'id','nickname')
                ->saving(function ($value){
                    return (int)$value;
                });

            $form->select('ad_category_id')
                 ->options(AdCategory::getOption())
                 ->required();

            $form->text('name')
                 ->maxLength(100)
                 ->required();
            $form->hidden('material');
            $form->radio('type')
                 ->options([1 => '图片',2 => '视频'])
                ->when(1,function (Form $form){
                    $form->image('material1','图片')
                        ->disk('oss')
                        ->autoUpload()
                        ->removable(false)
                        ->autoSave(false)
                        ->customFormat(function () use ($form){
                            if ($form->isEditing()){
                                return  $form->model()->material;
                            }
                            return  '';
                        })->uniqueName()
                        ->saveFullUrl()
                        ->saving(function ($value){
                            return (string)$value;
                        });
                })->when(2,function (Form $form){
                    $form->file('material2','视频')
                        ->disk('oss')
                        ->autoUpload()
                        ->removable(false)
                        ->autoSave(false)
                        ->uniqueName()
                        ->saveFullUrl()
                        ->customFormat(function () use ($form){
                            if ($form->isEditing()){
                                return  $form->model()->material;
                            }
                            return  '';
                        })
                        ->saving(function ($value){
                            return (string)$value;
                        });
                    $form->display('material','浏览视频')
                        ->customFormat(function ($value){
                            $html = '<video width="320" height="240" controls>
    <source src="'.$value.'" type="video/mp4">
</video>';
                            return $html;
                        });


                })->required();
            $form->select('ad_hierarchy_id')
                 ->options(AdHierarchy::getOption())
                 ->required();
            $form->text('area')
                 ->required();
            $form->number('days')
                 ->min(1)
                 ->required();
            $form->currency('amount')
                 ->symbol('￥')
                 ->required();
            $form->textarea('instructions')
                 ->saving(function ($value){
                    return (string)$value;
                 });

            $form->hidden('put_time');
            $form->hidden('end_time');
            $form->radio('audit_status')
                ->options([1 => '待审核',2 => '已通过',3 => '已拒绝'])
                ->when(3,function (Form $form){
                    $form->textarea('reject_reason','拒绝原因')
                        ->saving(function ($value){
                            return (string)$value;
                        })->rules('max:255',['max' => '最多500字']);
                })->required();
            $form->radio('status')
                 ->options([1 => '待付款',2 => '待投放',3 => '投放中',4 => '已完成',5 => '已取消'])
                 ->required();
            $form->hidden('trade_no');
            $form->saving(function (Form $form){
                if ($form->input('status')==3){
                    $form->input('put_time',date('Y-m-d H:i:s'));
                    $form->input('end_time',date('Y-m-d H:i:s',strtotime('+'.$form->input('days').' days')));
                }

                if ($form->input('type')==1){
                    $form->input('material',$form->input('material1'));
                }else{
                    $form->input('material',$form->input('material2'));
                }

                $form->deleteInput(['material1','material2']);

                if($form->isCreating()){
                    $form->input('trade_no',getTradeNo());
                }


            });

            $form->saved(function (Form $form){
                if ($form->input('user_id') && $form->input('audit_status')==2){
                    event(new \App\Events\Message([
                        'user_id' =>  $form->input('user_id'),
                        'type' => 2,
                        'content' => '恭喜您发布的广告已通过审核'
                    ]));
                }

                if ($form->input('user_id') && $form->input('audit_status')==3){
                    event(new \App\Events\Message([
                        'user_id' =>  $form->input('user_id'),
                        'type' => 2,
                        'content' => '您发布的广告未通过审核'
                    ]));
                }

            });



            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
